package com.shqcjd.sinopecdemomobile.ui.fragment.expertanswers;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shqcjd.sinopecdemomobile.R;
import com.shqcjd.sinopecdemomobile.api.ApiService;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxObserver;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.sinopecdemomobile.model.ResponseData;
import com.shqcjd.sinopecdemomobile.model.pojo.ExpertReplyListBean;
import com.shqcjd.sinopecdemomobile.ui.activity.ExpertAnswersDetailsActivity;
import com.shqcjd.sinopecdemomobile.ui.activity.problem.ProblemDetailsActivity;
import com.shqcjd.sinopecdemomobile.ui.adapter.UnansweredAdapter;
import com.shqcjd.sinopecdemomobile.ui.base.BaseFragment;
import com.shqcjd.sinopecdemomobile.ui.base.BasePresenter;
import com.shqcjd.sinopecdemomobile.util.SpacesItemDecoration;
import com.shqcjd.sinopecdemomobile.util.ToastUtils;

import java.util.List;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

/**
 * Created by sy on 2019/5/14.
 */

public class UnansweredFragment extends BaseFragment implements BaseQuickAdapter.RequestLoadMoreListener{
    private static final int PAGE_SIZE = 7;
    private static final String REPLY_STATUS = "reply_Status";
    private int mNextRequestPage = 1;
    @BindView(R.id.rv_unanswered_list)
    RecyclerView rv_unanswered_list;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;

    private Context mContext;
    private UnansweredAdapter unansweredAdapter;
    private View notDataView;
    private String replyStatus;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    public static UnansweredFragment newInstance(String type) {
        Bundle bundle = new Bundle();
        bundle.putString(REPLY_STATUS, type);
        UnansweredFragment contentFragment = new UnansweredFragment();
        contentFragment.setArguments(bundle);
        return contentFragment;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_unanswered_layout;
    }

    @Override
    public void initView(View rootView) {
        mContext = getContext();
        replyStatus = getArguments().getString(REPLY_STATUS);
        notDataView = getLayoutInflater().inflate(R.layout.layout_no_data, (ViewGroup) rv_unanswered_list.getParent(), false);
        initRefreshLayout();
        swipeLayout.setRefreshing(true);
        initProblem();


    }

    @Override
    public void initData() {
        refresh();
    }

    @Override
    public void onResume() {
        super.onResume();
        refresh();
    }

    private void initProblem() {

        unansweredAdapter = new UnansweredAdapter();
        rv_unanswered_list.setLayoutManager(new LinearLayoutManager(getContext()));
        rv_unanswered_list.addItemDecoration(new SpacesItemDecoration(0, 1, ContextCompat.getColor(getContext(), R.color.line)));
        rv_unanswered_list.setAdapter(unansweredAdapter);
        unansweredAdapter.setOnLoadMoreListener(this, rv_unanswered_list);
        unansweredAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent;
                ExpertReplyListBean.ListBean item = (ExpertReplyListBean.ListBean) adapter.getItem(position);
                if (replyStatus.equals("0")) {//未解答
                    intent = new Intent(getContext(), ExpertAnswersDetailsActivity.class);
                    intent.putExtra("problemFeedbackId",item.problemFeedbackId);
                    startActivity(intent);
                } else if (replyStatus.equals("1")){//已解答
                    intent = new Intent(mContext, ProblemDetailsActivity.class);
                    intent.putExtra("problemFeedbackId",item.problemFeedbackId);
                    startActivity(intent);
                }

            }
        });
    }

    private void initRefreshLayout() {
        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
    }

    private void refresh() {
        mNextRequestPage = 1;
        unansweredAdapter.setEnableLoadMore(false);//这里的作用是防止下拉刷新的时候还可以上拉加载
        ApiService.expertReplyList(mNextRequestPage,PAGE_SIZE,replyStatus)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<ExpertReplyListBean>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                    }

                    @Override
                    public void _onNext(ResponseData<ExpertReplyListBean> retData) {
                        if (retData != null && retData.retCode == 10000) {
                            setData(true, retData.data.list);
                        } else {
                            ToastUtils.showShort(mContext, retData.retMsg);
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        ToastUtils.showShort(mContext, errorMessage);
                        unansweredAdapter.setEnableLoadMore(true);
                        swipeLayout.setRefreshing(false);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        unansweredAdapter.setEnableLoadMore(true);
                        swipeLayout.setRefreshing(false);
                    }
                });
    }

    private void loadMore(){
        ApiService.expertReplyList(mNextRequestPage,PAGE_SIZE,replyStatus)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<ExpertReplyListBean>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                    }

                    @Override
                    public void _onNext(ResponseData<ExpertReplyListBean> retData) {
                        if (retData != null && retData.retCode == 10000) {
                            boolean isRefresh =mNextRequestPage ==1;
                            setData(isRefresh, retData.data.list);
                        } else {
                            ToastUtils.showShort(mContext, retData.retMsg);
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        ToastUtils.showShort(mContext, errorMessage);
                        unansweredAdapter.loadMoreFail();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }

    @Override
    public void onLoadMoreRequested() {
        loadMore();
    }

    private void setData(boolean isRefresh, List data) {

        mNextRequestPage++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            if (size > 0) {
                unansweredAdapter.setNewData(data);
            } else {
                unansweredAdapter.setNewData(null);
                unansweredAdapter.setEmptyView(notDataView);
            }
        } else {
            if (size > 0) {
                unansweredAdapter.addData(data);
            }
        }
        if (size < PAGE_SIZE) {
            //第一页如果不够一页就不显示没有更多数据布局
            unansweredAdapter.loadMoreEnd(isRefresh);
        } else {
            unansweredAdapter.loadMoreComplete();
        }
    }


}
